<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller
{
	public function __construct()
    {
		parent::__construct();
		$this->load->library('validation');
		include_once("inc/jqgrid_dist.php");
	}
	
	public function index()
	{
		$logged_in = $this->validation->check_session();
		$data = array();
		if($logged_in!=null)
			$data['login'] = true;
		$this->load->view('common/header', $data);
		if($logged_in!=null)
			$this->load->view('home', $data);
		else
			$this->load->view('login', $data);
		$this->load->view('common/footer', $data);
	}
	
	public function form($page)
	{
		$logged_in = $this->validation->check_session();
		$data = array();
		if($logged_in!=null)
			$data['login'] = true;
		if($logged_in!=null)
		{
			// If already logged in
			$data['login'] = true;
			// PHP Grid
			$g = new jqgrid();
			$data['g'] = $g;
		}
		
		if($page=="rajut")
		{
			$sql = "select distinct kelompok from karyawan where karyawan.kelompok != ''";
			$data["query_kelompok"] = $this->db->query($sql);
			$sql = "select distinct kode, nama from barang where barang.aktif=1 order by nama";
			$data["query_barang"] = $this->db->query($sql);
			$sql = "select kode, nama, kelompok, gaji, gteknisi from karyawan where karyawan.aktif=1 order by karyawan.nama";
			$data["query_karyawan"] = $this->db->query($sql);
		}
		else if($page=="sales")
		{
			$sql = "select distinct kode, nama from barang where barang.aktif=1 order by nama";
			$data["query_barang"] = $this->db->query($sql);
		}
		
		$this->load->view('common/header', $data);
		if($logged_in!=null)
			$this->load->view('form/'.$page, $data);
		else
			redirect(base_url());	// Credential Error
		$this->load->view('common/footer', $data);
	}
	
	public function report($page, $message = null)
	{
		$logged_in = $this->validation->check_session();
		$data = array();
		if($logged_in!=null)
			$data['login'] = true;
		if($message != null)
			$data['message'] = str_replace("%20", " ", $message);
		if($page == "harian")
		{
			$sql = "select distinct kode, nama from barang where barang.aktif=1 order by nama";
			$data["query_barang"] = $this->db->query($sql);
		}
		else if($page == "stock")
		{
			$sql = "select s.waktu, b.kode, b.nama as tipe, s.jumlah from stock s inner join barang b on s.tipe=b.kode where b.aktif=1 order by b.nama";
			$data["query"] = $this->db->query($sql)->result();
		}
		$this->load->view('common/header', $data);
		$this->load->view('report/'.$page, $data);
		$this->load->view('common/footer', $data);
	}
	
	public function detail($nolap)
	{
		$logged_in = $this->validation->check_session();
		$data = array();
		$data['nolap'] = $nolap;
		if($logged_in!=null)
			$data['login'] = true;
		$sql_rajut = "select d.nolap, x.tanggal, b.nama as tipe, k1.nama as nama1, k2.nama as nama2, k3.nama as nama3, d.rajut, x.keterangan from rajut x inner join rajut_det d on x.`No`=d.`No` inner join barang b on d.barang=b.kode inner join karyawan k1 on x.nama1=k1.kode inner join karyawan k2 on x.nama2=k2.kode inner join karyawan k3 on x.nama3=k3.kode where d.nolap = '$nolap'";
		$sql_obras = "select d.nolap, x.tanggal, b.nama as tipe, k.nama, SUM(d.jumlah) as jumlah, x.keterangan from obras x inner join obras_det d on x.`No`=d.`No` inner join barang b on d.barang=b.kode inner join karyawan k on x.nama=k.kode where d.nolap = '$nolap' group by d.nolap, x.tanggal, b.nama, k.nama, x.keterangan ";
		$sql_setter = "select d.nolap, x.tanggal, b.nama as tipe, k.nama, SUM(d.jumlah) as jumlah, x.keterangan from setter x inner join setter_det d on x.`No`=d.`No` inner join barang b on d.barang=b.kode inner join karyawan k on x.nama=k.kode where d.nolap = '$nolap' group by d.nolap, x.tanggal, b.nama, k.nama, x.keterangan ";
		$data["query_rajut"] = $this->db->query($sql_rajut);
		$data["query_obras"] = $this->db->query($sql_obras);
		$data["query_setter"] = $this->db->query($sql_setter);
		$this->load->view('common/header', $data);
		$this->load->view('report/detail', $data);
		$this->load->view('common/footer', $data);
	}
	
	function login()
	{
		return $this->validation->login();
		redirect(base_url());
	}
	
	function logout()
	{
		return $this->validation->logout();
		redirect(base_url());
	}
}
/* End of file main.php */
/* Location: ./application/controllers/main.php */